/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.quiz;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author User
 */
public class AnswerServiceImpl implements AnswerService {

    private AnswerDAO answerDAO;
    private QuestionDAO questionDAO;
    private TitleQuizDAO titleQuizDAO;

    public void setAnswerDAO(AnswerDAO answerDAO) {
        this.answerDAO = answerDAO;
    }

    public void setQuestionDAO(QuestionDAO questionDAO) {
        this.questionDAO = questionDAO;
    }
    
    public void setTitleQuizDAO(TitleQuizDAO titleQuizDAO) {
        this.titleQuizDAO = titleQuizDAO;
    }

    public void saveAnswer(Answer answer) throws Exception {
        this.answerDAO.save(answer);
    }

    public void deleteAnswer(Answer answer) throws Exception {
        this.answerDAO.delete(answer);
    }

    public void updateAnswer(Answer answer) throws Exception {
        this.answerDAO.update(answer);
    }

    public List loadAll() throws Exception {
        return this.answerDAO.getAll(Answer.class);
    }

    public Answer loadById(long id) throws Exception {
        return (Answer) this.answerDAO.load(Answer.class, id);
    }

    public void saveQuestionAnswer(Question question, List list) throws Exception {
        this.questionDAO.save(question);
        this.questionDAO.getSessionByGeneric().getSessionFactory().getCurrentSession().flush();
        for (int x = 0; x < list.size(); x++) {
            Answer ans = (Answer) list.get(x);
            ans.setQuestion(question);
            this.answerDAO.save(ans);
        }
    }

    @SuppressWarnings("unchecked")
    public List getQuestionAndAnswerByTitle(long titleId) throws Exception {
        List listResult = new ArrayList();
        List listQuestion = this.questionDAO.getQuestionByTitle(titleId);
        List listAnswer = this.answerDAO.getAnswerByTitle(String.valueOf(titleId));
        int count = 0;
        int lock = 0;
        if (listQuestion.size() > 0 && listAnswer.size() > 0) {
            listResult.add(listQuestion.get(0));
            for (int x = 0; x < listAnswer.size(); x++) {
                count += 1;
                listResult.add(listAnswer.get(x));
                if (count == 5) {
                    count = 0;
                    lock += 1;
                    if (lock < listQuestion.size()) {
                        listResult.add(listQuestion.get(lock));
                    }
                }
            }
        }
        return listResult;
    }

    public List getQuestionAndAnswerByQuestion(long questionId) throws Exception {
        List listResult = new ArrayList();
        Question question = (Question) this.questionDAO.load(Question.class, questionId);
        List listAnswer = this.answerDAO.getAnswerByQuestion(questionId);
        listResult.add(0,question);
        listResult.add(1,listAnswer);        
        return listResult;
    }

    public void updateQuestionAnswer(Question question, List list) throws Exception {    
        Question questionUpdate = (Question) this.questionDAO.load(Question.class, question.getId());
        questionUpdate.setDescription(question.getDescription());
        questionUpdate.setUpdateBy(question.getUpdateBy());
        questionUpdate.setUpdateDt(question.getUpdateDt());
        this.questionDAO.update(questionUpdate);        
        for(int x=0;x<list.size();x++){
            Answer answer = (Answer)list.get(x);
            Answer answerUpdate = (Answer)this.answerDAO.load(Answer.class, answer.getId());
            answerUpdate.setDescription(answer.getDescription());
            answerUpdate.setScore(answer.getScore());
            answerUpdate.setUpdateBy(answer.getUpdateBy());
            answerUpdate.setUpdateDt(answer.getUpdateDt());
            this.answerDAO.update(answerUpdate);
        }
    }

    public void deleteByQuestion(long questionId) throws Exception {
        this.answerDAO.deleteByQuestion(questionId);
        this.answerDAO.getSessionByGeneric().getSessionFactory().getCurrentSession().flush();
        Question question= (Question) this.questionDAO.load(Question.class, questionId);
        this.questionDAO.delete(question);        
    }

    public void deleteByTitle(long titleId) throws Exception {
       this.answerDAO.deleteByTitle(String.valueOf(titleId));
       this.answerDAO.getSessionByGeneric().getSessionFactory().getCurrentSession().flush();
       this.questionDAO.deleteByTitle(titleId);
       this.questionDAO.getSessionByGeneric().getSessionFactory().getCurrentSession().flush();
       TitleQuiz titleQuiz = (TitleQuiz) this.titleQuizDAO.load(TitleQuiz.class, titleId);
       this.titleQuizDAO.delete(titleQuiz);
       
    }

  

    

   
}
